package com.code.wanmi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.code.wanmi.entity.wm_user.WmSysUser;
import com.code.wanmi.util.DataSource;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * <p>
 * 用户信息表 Mapper 接口
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-12-05
 */
@Mapper
public interface WmSysUserMapper extends BaseMapper<WmSysUser> {
    /**
     * 查询用户列表
     */
    @DataSource("wmuser")
    @Select("select `user_id`,`user_name`,`user_type`,`email`,`phonenumber`,`sex`,`salt`,`status`,\n" +
            "`login_ip`,`login_date`,`create_by`,`create_time`,`update_by`,`update_time`,`remark` from `wm_sys_user`")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "user_type", property = "userType"),
            @Result(column = "login_ip", property = "loginIp"),
            @Result(column = "login_date", property = "loginDate"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
    })
    List<WmSysUser> selectUsers();

    /**
     * 删除某个用户
     **/
    @DataSource("wmuser")
    @Update("update `wm_sys_user` set `del_flag`=2,`status`=0 where `user_id`=#{userId}")
    @Results({
            @Result(column = "`del_flag`", property = "delFlag"),
            @Result(column = "`user_id`", property = "userId")
    })
    Integer updateuser(Long userId);

    @DataSource("wmuser")
    @Insert({"INSERT INTO wm_sys_user " +
            "(login_name,user_name,email,phonenumber,sex,`password`,salt,create_by,create_time) \n" +
            "VALUES(#{loginName},#{userName},#{email},#{phonenumber},#{sex},#{password},#{salt},#{createBy},NOW())"})
    @Results({
            @Result(column = "login_name", property = "loginName"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
    })
    Integer insertUser(WmSysUser user);

    @DataSource("wmuser")
    @Select({"SELECT user_id,login_name,user_name,email,phonenumber,sex,avater,password,salt,status,del_flag,login_ip,login_date,create_by,create_time,update_by,uodate_time,remake from wm_sys_user WHERE login_name = #{loginName} limit 1"})
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "login_name", property = "loginName"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "user_type", property = "userType"),
            @Result(column = "login_ip", property = "loginIp"),
            @Result(column = "login_date", property = "loginDate"),
            @Result(column = "create_by", property = "userName"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "del_flag", property = "delFlag")
    })

    WmSysUser selectUser(WmSysUser user);

    @DataSource("wmuser")
    @Select({"SELECT user_id,login_name,user_name,email,phonenumber,sex,avater,password,salt,status,del_flag,login_ip,login_date,create_by,create_time,update_by,uodate_time,remake FROM wm_sys_user WHERE \n" +
            "login_name = #{loginName} AND `password` = #{password} AND `status`=1"})
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "login_name", property = "loginName"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "user_type", property = "userType"),
            @Result(column = "login_ip", property = "loginIp"),
            @Result(column = "login_date", property = "loginDate"),
            @Result(column = "create_by", property = "userName"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "del_flag", property = "delFlag")
    })
    WmSysUser selectUserByUsernameAndPassword(WmSysUser user);

    @DataSource("wmuser")
    @Update({"UPDATE wm_sys_user SET login_ip=#{loginIp}, login_date=NOW() \n" +
            "WHERE login_name = #{loginName}"})
    @Results({
            @Result(column = "login_name", property = "loginName"),
            @Result(column = "login_ip", property = "loginIp"),
            @Result(column = "login_date", property = "loginDate"),
    })
    Integer updateLoginIP(WmSysUser user);

    /**
     * 修改用户信息
     *
     * @return
     */
    @DataSource("wmuser")
    @UpdateProvider(type = UserSqlBuilder.class, method = "updateUser")
    Integer updateUser(WmSysUser wmSysUser);

    /**通过登录名ID查询用户信息*/
    @DataSource("wmuser")
    @Select("select `user_id`,`user_name`,`user_type`,`email`,`phonenumber`,`sex`,`salt`,`status`,\n" +
            "`login_ip`,`login_date`,`create_by`,`create_time`,`update_by`,`update_time`,`remark` from `wm_sys_user` " +
            "where user_id = #{userId} and login_name = #{loginName}")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "user_type", property = "userType"),
            @Result(column = "login_ip", property = "loginIp"),
            @Result(column = "login_date", property = "loginDate"),
            @Result(column = "create_by", property = "userName"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
    })
    WmSysUser selectUserByIdAndLoginName(WmSysUser wmSysUser);


    class UserSqlBuilder {
        // If not use @Param, you should be define same arguments with mapper method
        public static String updateUser(final WmSysUser wmSysUser) {
            return new SQL() {{
                UPDATE("wm_sys_user");
                if (wmSysUser.getEmail() != null)
                    SET("email = #{email}");
                if (wmSysUser.getPhonenumber() != null)
                    SET("phonenumber = #{phonenumber}");
                if (wmSysUser.getPassword() != null)
                    SET("password = #{password}");
                WHERE("user_id = #{userId}");
            }}.toString();
        }


        // If use @Param, you can define only arguments to be used
//        public static String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
//            return new SQL(){{
//                SELECT("*");
//                FROM("users");
//                WHERE("name like #{name} || '%'");
//                ORDER_BY(orderByColumn);
//            }}.toString();
//        }
    }
}
